package com.cennavi.mineplay.mineplay_data.model;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.jboss.logging.Logger;

public class ImportTrackPoint {
	
	private static Logger logger = Logger.getLogger(ImportTrackPoint.class);

	private Connection conn;
	
	private List<TrackPoint> ps;
	
	private String[] tableNames;
	
	private Connection sourceConnection;
	
	public ImportTrackPoint(Connection conn, List<TrackPoint> ps,String destTableNames) {
		super();
		this.conn = conn;
		this.ps = ps;
		String[] splits = destTableNames.split(",");
		this.tableNames = new String[splits.length];
		for(int i=0;i<tableNames.length;i++){
			tableNames[i] = splits[i];
		}
		
		tableNames[1] = TableCreateTask.getTodayTableName();
	}


	

	public Connection getSourceConnection() {
		return sourceConnection;
	}




	public void setSourceConnection(Connection sourceConnection) {
		this.sourceConnection = sourceConnection;
	}




	public void importData() throws Exception{
		
		String sql1 = "insert into "+tableNames[0]+"(car_number,company_id,geom,speed,direct,time,is_online) values"
				+ " (?,?,st_geomfromtext(?,4326),?,?,?,?) "
				+ "on conflict(car_number) do update set geom=st_geomfromtext(?,4326),speed=?,direct=?,time=?,is_online=?";
		
//		String sql2 = "insert into "+tableNames[1]+"(car_number,company_id,geom,speed,direct,time) "
//				+ "values (?,?,st_geomfromtext(?,4326),?,?,?)";
		
		String sql2 = "insert into "+tableNames[1]+"(car_number,company_id,geom,speed,direct,time,is_online) "
				+ "values (?,?,st_geomfromtext(?,4326),?,?,?,?) "
				+ "on conflict(car_number,time) do nothing";
		
		PreparedStatement pstmt = this.conn.prepareStatement(sql1);
		
		
		for(TrackPoint tp : ps){
			pstmt.setString(1, tp.getCar_number());
			pstmt.setInt(2, tp.getCompany_id());
			pstmt.setString(3, "Point("+ tp.getLng()+" "+tp.getLat()+")");
			pstmt.setDouble(4, tp.getSpeed());
			pstmt.setDouble(5, tp.getDirect());
			pstmt.setLong(6, tp.getTime());
			pstmt.setLong(7, tp.getIs_online());
			
			pstmt.setString(8, "Point("+ tp.getLng()+" "+tp.getLat()+")");
			pstmt.setDouble(9, tp.getSpeed());
			pstmt.setDouble(10, tp.getDirect());
			pstmt.setLong(11, tp.getTime());
			pstmt.setLong(12, tp.getIs_online());
			
			pstmt.addBatch();
		}
		
		int[] results = pstmt.executeBatch();
		
		Map<Integer,Integer> map = new HashMap<Integer,Integer>();
		
		for(int v:results){
			if (map.containsKey(v))
				map.put(v, map.get(v)+1);
			else
				map.put(v, 1);
		}
		
		StringBuilder sb = new StringBuilder();
		
		for(Entry<Integer,Integer> en : map.entrySet()){
			sb.append("key:"+en.getKey()+"->value:"+en.getValue()+",");
		}
		
		logger.info("connection:"+this.sourceConnection.getMetaData().getURL()+"=>"+sb);
		
		pstmt.close();
		
		pstmt = this.conn.prepareStatement(sql2);
		
		for(TrackPoint tp : ps){
			pstmt.setString(1, tp.getCar_number());
			pstmt.setInt(2, tp.getCompany_id());
			pstmt.setString(3, "Point("+ tp.getLng()+" "+tp.getLat()+")");
			pstmt.setDouble(4, tp.getSpeed());
			pstmt.setDouble(5, tp.getDirect());
			pstmt.setLong(6, tp.getTime());
			pstmt.setLong(7, tp.getIs_online());
			pstmt.addBatch();
		}
		
		pstmt.executeBatch();
		
		pstmt.close();
		
//		conn.close();
	}
	
}
